<?php
function mobile_service_retailstore_list($parameters)
{
    global $user;
    $start = $parameters['page_count'] * variable_get("list_page_item_number", 5);
    $query = db_select('addr', 'A');
    $query->join('retailstore', 'S', 'A.addr_id=S.addr_id');
    $query->fields('S');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    if (!empty($parameters['retailstore_name'])) {//模糊店铺名字查询条件
        $query->condition('S.name', '%' . db_like($parameters['retailstore_name']) . '%', 'LIKE');
    }
    if (!empty($parameters['county'])) {//县级
        $query->condition('A.county_id', $parameters['county'], '=');
    } elseif (!empty($parameters['city'])) {//市级
        $query->condition('A.city_id', $parameters['city'], '=');
    } elseif (!empty($parameters['province'])) {//省级
        $query->condition('A.province_id', $parameters['province'], '=');
    }
    if (!empty($parameters['telephone'])) {//模糊电话号码查询条件
        $query->condition('S.phone', '%' . db_like($parameters['telephone']) . '%', 'LIKE');
    }
    if(!user_access('view all retailstore') && user_access('view own retailstore')){
        $query->condition('S.author_uid',$user->uid, '=');
    }
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');


    $query->orderBy('S.retailstore_id', 'DESC');
    if (!empty($parameters['cursor']) && !empty($parameters['comparator'])) { //游标翻页
        $query->condition('S.retailstore_id', $parameters['cursor'], $parameters['comparator']);
    }
    $query->range(0,10);

    $results = $query->execute();
    $revisit_list = [];
    if(!empty($results)){
        foreach ($results as $result) {
            $revisit_list[] = $result;
        }
    }
    return  ["data"=>$revisit_list];

}
function mobile_service_retailstore_add($parameters)
{
    global $user;
    $module_name="retailstore";
    $id = !empty($parameters['retailstore_id'])?$parameters['retailstore_id']:'';
    try {
        if (empty($id)) {
            if(!user_access('add '.$module_name)){
                return ['option'=>0,'errorNum'=>3];
            }
            $nid = db_insert('retailstore')// Table name no longer needs {}
            ->fields(array(
                'addr_id' => $parameters['addr_id'],
                'addr_details' => $parameters['addr_details'],
                'name' => $parameters['name'],
                'phone' => $parameters['phone'],
                'sacfvariety_id' => $parameters['sacfvariety_id'],
                'width' => !empty($parameters['width']) ? $parameters['width'] : '0',
                'height' => !empty($parameters['height']) ? $parameters['height'] : '0',
                'plant_id' => $parameters['plant_id'],
                'brands' => $parameters['brand_id'],
                'farm_area' => $parameters['farm_area'],
//                'sales_1' => !empty($parameters['sales_1']) ? $parameters['sales_1'] : '0',
                'author_uid' => $user->uid,
                'timestamp' => time(),))
                ->execute();
            $result_option = (object)["option" => 1];
            return $result_option;

        } else {
            if(!mobile_user_edit_access($module_name,$module_name.'_id',$id,'edit own '.$module_name,'edit all '.$module_name)){
                return ['option'=>0,'errorNum'=>3];
            }
            $nid = db_update('retailstore')
                ->fields(array(
                    'addr_id' => $parameters['addr_id'],
                    'addr_details' => $parameters['addr_details'],
                    'name' => $parameters['name'],
                    'phone' => $parameters['phone'],
                    'sacfvariety_id' => $parameters['sacfvariety_id'],
                    'width' => !empty($parameters['width']) ? $parameters['width'] : '0',
                    'height' => !empty($parameters['height']) ? $parameters['height'] : '0',
                    'plant_id' => $parameters['plant_id'],
                    'brands' => $parameters['brand_id'],
                    'farm_area' => $parameters['farm_area'],
//                    'sales_1' => !empty($parameters['sales_1']) ? $parameters['sales_1'] : '0',
                    'timestamp' => time(),))
                ->condition('retailstore_id',$parameters['retailstore_id'],'=')
                ->execute();
            if ($nid > 0) {
                $result_option = (object)["option" => 1];
                return $result_option;
            }
            $result_option = (object)["option" => 0];
            return $result_option;
        }
    } catch (Exception $e) {
        $result_option = (object)["option" => 0, "error" => $e->getMessage()];
        return $result_option;
    }
}